//
//  YFCamera.m
//  CamerDemo
//
//  Created by Winny Zhang on 2019/4/1.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "YFCamera.h"
#import "YFCameraViewController.h"

@interface YFCamera ()

@end

@implementation YFCamera


+ (instancetype)camera {
    return [[self alloc] init];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initCamera];
    }
    return self;
}

- (void)initCamera {
    YFCameraViewController *camera = [YFCameraViewController new];
    camera.maxSeconds = 15;
    self.camera = camera;
}


- (void)show {
    UIViewController *vc = [YFCamera findCurrentShowingViewController];
    [vc presentViewController:_camera animated:YES completion:nil];
}

/**
 完成回调

 @param imageCompletion 图片回调
 @param videoCompletion 视频回调
 */
- (void)completedWithImage:(void(^)(UIImage * _Nullable image))imageCompletion
                 withVideo:(void(^)(UIImage * _Nullable firstFrameImage, NSURL * _Nullable videoURL, long long duration))videoCompletion {
    self.camera.completion = ^(BOOL isImage, UIImage * _Nullable image, NSURL * _Nullable videoURL, UIImage * _Nullable videoFirstFrameImage, long long duration) {
        if (isImage) {
            if (imageCompletion) {
                imageCompletion(image);
            }
        } else {
            if (videoCompletion) {
                videoCompletion(videoFirstFrameImage, videoURL, duration);
            }
        }
    };
}

#pragma mark - Setter
- (void)setMaxSeconds:(NSInteger)maxSeconds {
    _maxSeconds = maxSeconds;
    self.camera.maxSeconds = maxSeconds;
}
- (void)setIsTakePhoto:(BOOL)isTakePhoto {
    _isTakePhoto = isTakePhoto;
    self.camera.isTakePhoto = isTakePhoto;
}

#pragma mark - 查找当前控制器
+ (UIViewController *)findCurrentShowingViewController {
    //获得当前活动窗口的根视图
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentShowingVC = [self findCurrentShowingViewControllerFrom:vc];
    return currentShowingVC;
}

//注意考虑几种特殊情况：①A present B, B present C，参数vc为A时候的情况
/* 完整的描述请参见文件头部 */
+ (UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc
{
    //方法1：递归方法 Recursive method
    UIViewController *currentShowingVC;
    if ([vc presentedViewController]) { //注要优先判断vc是否有弹出其他视图，如有则当前显示的视图肯定是在那上面
        // 当前视图是被presented出来的
        UIViewController *nextRootVC = [vc presentedViewController];
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
        
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController];
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
        
    } else if ([vc isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
        
    } else {
        // 根视图为非导航类
        currentShowingVC = vc;
    }
    
    return currentShowingVC;
}

@end
